package com.flazy;

import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class BrowserBars extends JPanel {
	private static final long serialVersionUID = 1819130770269101476L;
	private String noticeFilePath = BrowserFrame.noticeFilePath;//公告文件路径
	private JWebBrowser webBrowser;//浏览器窗口
	private JButton homeBtn;//主页按钮
	private JButton backBtn;//后退按钮
	private JButton forwardBtn;//前进按钮
	private JPanel toolPanel;//按钮容器
	private JLabel notice;//公告标签
	/**
	 * 工具按键
	 */
	public BrowserBars() {
		setBounds(0, 0, 200, 24);//设置工具条宽和高
		setLayout(new BorderLayout(0, 0));//设置工具条布局

		toolPanel = new JPanel();//创建按钮容器
		toolPanel.setBounds(0, 0, 100, 24);//设置按钮容器宽和高
		toolPanel.setBackground(Color.DARK_GRAY);//设置按钮容器背景
		toolPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 6, 2));//设置按钮容器布局
		
		homeBtn = new JButton("");//创建主页按钮
		homeBtn.setIcon(new ImageIcon(BrowserFrame.class.getResource("/com/flazy/resource/home_wev8.gif")));//设置按钮图标
		homeBtn.setPreferredSize(new Dimension(20, 20));//设置按钮宽高
		homeBtn.setBorder(null);//设置按钮边框
		homeBtn.addActionListener(new ActionListener() {//添加按钮事件
			public void actionPerformed(ActionEvent e) {//绑定按钮事件
				webBrowser = BrowserPanel.getWebBrowser();//得到浏览器对象
				webBrowser.navigate(BrowserPanel.getHomePage());//跳转到浏览器主页地址
			}
		});

		backBtn = new JButton("");//创建后退按钮
		backBtn.setIcon(new ImageIcon(BrowserFrame.class.getResource("/com/flazy/resource/left_wev8.png")));//设置按钮图标
		backBtn.setPreferredSize(new Dimension(20, 20));//设置按钮宽高
		backBtn.setBorder(null);//设置按钮边框
		backBtn.addActionListener(new ActionListener() {//添加按钮事件
			public void actionPerformed(ActionEvent e) {//绑定按钮事件
				webBrowser = BrowserPanel.getWebBrowser();//得到浏览器对象
				webBrowser.navigateBack();//调用浏览器执行后退方法
			}
		});

		forwardBtn = new JButton("");//创建前进按钮
		forwardBtn.setIcon(new ImageIcon(BrowserFrame.class.getResource("/com/flazy/resource/right_wev8.png")));//设置按钮图标
		forwardBtn.setPreferredSize(new Dimension(20, 20));//设置按钮宽高
		forwardBtn.setBorder(null);//设置按钮边框
		forwardBtn.addActionListener(new ActionListener() {//添加按钮事件
			public void actionPerformed(ActionEvent e) {//绑定按钮事件
				webBrowser = BrowserPanel.getWebBrowser();//得到浏览器对象
				webBrowser.navigateForward();//调用浏览器执行前进方法
			}
		});
		
		toolPanel.add(homeBtn);//将主页按钮添加到按钮容器中
		toolPanel.add(backBtn);//将后退按钮添加到按钮容器中
		toolPanel.add(forwardBtn);//将前进按钮添加到按钮容器中
		add(toolPanel,BorderLayout.EAST);//将按钮容器添加到左边
		
		notice = new JLabel("");//创建公告标签
		notice.setForeground(Color.DARK_GRAY);//设置字体颜色
		notice.setFont(new Font("宋体", Font.PLAIN, 11));//设置公告字体
		add(notice, BorderLayout.CENTER);//将公告标签添加到中间
		
		String noticeText = this.readNoticeText(noticeFilePath);//读取公告文件内容
		noticeText = "".equals(noticeText)?"没有公告信息!":noticeText;
		notice.setText(noticeText);//设置公告内容
	}
	/**
	 * 读取本地公告文件
	 */
	private String readNoticeText(String noticeFilePath) {
		StringBuffer buffer = new StringBuffer();
		try {
	        File noticeFile = new File(noticeFilePath);
	        if(noticeFile.exists()) {
		        List<String> lineList = Files.readLines(noticeFile, Charsets.UTF_8);
		        for (String line : lineList) {
		        	buffer.append(line);
		        }
	        }
		} catch (IOException e) {
			e.printStackTrace();
		}
	    return buffer.toString();   
	}
}
